package com.atnjupt.servlet;

import com.atnjupt.util.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author Wizarder
 * @create 2021-06-08 16:43
 */
public class CookieServlet extends BaseServlet{
    protected void testPath(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie1 = new Cookie("path1","path1");
        //request.getContextPath()获取工程路径 ==> /工程名
        cookie1.setPath(request.getContextPath());
        Cookie cookie2 = new Cookie("path2","path2");
        cookie2.setPath(request.getContextPath()+"/abc");
        response.addCookie(cookie1);
        response.addCookie(cookie2);
    }
    protected void defaultLife(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("life3600","life3600");
        cookie.setMaxAge(-1);//设置默认存活时间，关闭浏览器就删除
        response.addCookie(cookie);
        response.getWriter().write("创建了一个默认存活的Cookie");
    }
    protected void deleteNow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1先找到修改的Cookie
        Cookie cookie = CookieUtils.findCookies("key1", request.getCookies());
        if(cookie != null){
            cookie.setMaxAge(0);
            //发送给客户端进行删除
            response.addCookie(cookie);
            response.getWriter().write("key1-Cookie已经删除");
        }

    }
    protected void life3600(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("life3600","life3600");
        cookie.setMaxAge(60*60);//设置存活时间
        response.addCookie(cookie);
        response.getWriter().write("创建了存活一个小时的Cookie");
    }
    protected void createCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、创建cookie对象
        Cookie cookie = new Cookie("key1","value1");
        //2、服务器通知客户端保存cookie！！！不能省略
        response.addCookie(cookie);
        Cookie cookie1 = new Cookie("key2","value2");
        response.addCookie(cookie1);


        response.getWriter().write("Cookie创建了!");
    }
    protected void getCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //request.getCookies();方法返回的是Cookie数组
        Cookie[] cookies = request.getCookies();
        //获取所有的Cookie信息
        for (Cookie cookie : cookies) {
            response.getWriter().write("Cookie[" + cookie.getName() + "=" + cookie.getValue() + "]<br/>");
        }
        //查找需要的Cookie信息
        Cookie iWantCookie = null;
        iWantCookie = CookieUtils.findCookies("key1", cookies);
        if(iWantCookie != null){
            response.getWriter().write("找到了想要的Cookie，值为：" + iWantCookie.getValue());
        }


    }
    protected void updateCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //方法一
        //重新创建一个key值相同的Cookie对象
//        Cookie cookie = new Cookie("key1","newValue1");
        //通过response发送给客户端保存
//        response.addCookie(cookie);
//        response.getWriter().write("key1的Cookie修改好了");
        //方法二
        //先获取要修改的Cookie对象
        Cookie cookie = CookieUtils.findCookies("key2", request.getCookies());
        if (cookie != null){
            cookie.setValue("newValue2");
            //条用response.addCookie()告诉客户端保存
            response.addCookie(cookie);
        }

        response.getWriter().write("key2的Cookie修改好了");


    }
}
